<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Act extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('date');
	}

	public	function Error($msg) {
		echo("
   				<Script>
   				window.alert(\"$msg\")
   				history.go(-1)
   				</Script>"); 
		exit;

	}

	public function act_make($username,$email){
		$this->load->helper('string');


		$act_str=random_string('alnum',8);
		$act_time=now();
		$act_key=md5($username.$act_str.$act_time);
		//echo $act_key.'<br/>';
		$act_data=array(
			'username'=>$username,
			'act_key'=>$act_key,
			'time'=>now()
		);
		$key_url='<a href="http://cireg.com/act/act_check?key='.$act_key.'">点此激活帐号 '.$username.'<a> 链接5分钟内有效';
		$sql = "SELECT *  FROM act_data WHERE username = ? ";
		$binds = array($username);
		$result = $this->db->query($sql, $binds)->result();
		if(count($result)==0){
			$this->toemail($email, $key_url);
			$query=$this->db->insert_string('act_data',$act_data);
			$this->db->query($query);
			echo "<script language='javascript' type='text/javascript'>";
			echo 'alert("发送成功请注意查收");';
			echo "</script>";
			redirect('index','refresh');
		}else{

			$this->toemail($email,$key_url);
			$where="username = '".$username."'";
			$query=$this->db->update_string('act_data', $act_data, $where);
			$this->db->query($query);
			echo "<script language='javascript' type='text/javascript'>";
			echo 'alert("发送成功请注意查收");';
			echo "</script>";
			redirect('index','refresh');
		}

	}
	public function toemail($email,$msg){
		//echo $msg;
		$this->load->library('email');
		$this->email->from('administrator@microsoft.com', 'Microsoft Corporation');
		$this->email->to($email);
		$this->email->subject('来自cireg.com的激活码');
		$this->email->message($msg);
		$this->email->send();

	}
	public function index()
	{
		$this->load->view('act');
	}
	public function act_send(){
		$username=$_POST['username'];
		$email=$_POST['email'];
		$sql = "SELECT *  FROM user WHERE username = ? ";
		$binds = array($username);
		$result = $this->db->query($sql, $binds);
		if(count($result->result())==0){
			$this->Error('不存在的用户名！');
		}else{
			if($result->row()->activation==1){
				$this->Error('用户已激活,请勿重复激活！');
			}else{
				$this->act_make($username, $email);
			}
		}
	}

	public function  act_check(){
		$act_key=$_GET['key'];
		$sql = "SELECT *  FROM act_data WHERE act_key = ? ";
		$binds = array($act_key);
		$result = $this->db->query($sql, $binds);
		if(count($result->result())==0){
			$this->Error('错误的激活码！');
		}else{
			if(now()-$result->row()->time < 300){
				$where="username = '".$result->row()->username."'";
				$query=$this->db->update_string('user', array('activation'=>1,'acttime'=>unix_to_human(now(), TRUE,'PRC')), $where);
				$this->db->query($query);
				$query=$this->db->insert_string('event_log',array('username'=>$result->row()->username,'event'=>'activation','time'=>unix_to_human(now(), TRUE,'PRC')));
				$this->db->query($query);




				$this->db->delete('act_data',array('username'=>$result->row()->username));

				echo "<script language='javascript' type='text/javascript'>";
				echo 'alert("用户  '.$result->row()->username.'  激活成功");';
				echo "</script>";
				redirect('index','refresh');
			}else{
				$this->Error('激活码已过期，请重新激活！');
			}

		}

	}

}
?>